package com.tmp.alarm.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jd.alarm.model.JobAlarmModel;
import com.jd.alarm.model.QueueAlarmModel;

public class FileUtil {
	private final static Logger log = LoggerFactory.getLogger(FileUtil.class);

	public static void readFileInsertMysqlThenDel(String filepath, String insertSql, Connection conn)
			throws FileNotFoundException, IOException {
		try {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
				try {
					if (file.getName().startsWith("mart_mobile_job")) {
						log.info("开始job信息的处理.......");
						doJob(br, insertSql, conn);
						log.info("job信息的处理成功.......");
						file.delete();
						log.info(file.getAbsolutePath() + "已删除");
					} else if (file.getName().startsWith("mart_mobile_queue")) {
						log.info("开始queue信息的处理.......");
						doQueue(br, insertSql, conn);
						log.info("queue信息的处理成功.......");
						file.delete();
						log.info(file.getAbsolutePath() + "已删除");
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					br.close();
				}
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				if (filelist.length==0) {
					log.warn("本次读取的文件夹为空");
				}
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + File.separator + filelist[i]);
					if (!readfile.isDirectory()) {
						log.info("开始读取并处理:" + readfile.getName());
						BufferedReader br = new BufferedReader(new FileReader(readfile));// 构造一个BufferedReader类来读取文件
						try {
							if (readfile.getName().startsWith("mart_mobile_job")) {
								log.info("开始job信息的处理.......");
								doJob(br, insertSql, conn);
								log.info("job信息的处理成功.......");
								readfile.delete();
								log.info(readfile.getAbsolutePath() + "已删除");
							} else if (readfile.getName().startsWith("mart_mobile_queue")) {
								log.info("开始queue信息的处理.......");
								doQueue(br, insertSql, conn);
								log.info("queue信息的处理成功.......");
								readfile.delete();
								log.info(readfile.getAbsolutePath() + "已删除");
							}
						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							br.close();
						}
					} else if (readfile.isDirectory()) {
						readFileInsertMysqlThenDel(filepath + File.separator + filelist[i], insertSql, conn);
					}
				}

			}

		} catch (FileNotFoundException e) {
			log.error("readFileInsertMysqlThenMove()   Exception:" + e.getMessage());
		}
	}

	private static void doQueue(BufferedReader br, String insertSql, Connection conn) throws SQLException, IOException {
		conn.setAutoCommit(false); // 设置手动提交
		int count = 0;
		PreparedStatement psts = conn.prepareStatement(insertSql);
		log.info("准备执行的sql语句:" + insertSql);
		String line = null;
		while (null != (line = br.readLine())) {
			if (StringUtils.isBlank(line)) {
				log.warn("本条数据为空");
				continue;
			}
			QueueAlarmModel queue = new QueueAlarmModel(line);
			if (queue != null && StringUtils.isNotBlank(queue.getQueue_name())) {
				psts.setString(1, queue.getLog_time());// log_time
				psts.setString(2, "1");// type
				psts.setString(3, queue.getQueue_name());// queue_name
				psts.setString(4, queue.getUsed_memory());// used_memory
				psts.setString(5, queue.getUsed_vcore());// used_vcore
				psts.setString(6, queue.getActive_job_num());// active_job_num
				psts.setString(7, queue.getPending_job_num());// pending_job_num
				psts.setString(8, queue.getMin_memory());// min_memory
				psts.setString(9, queue.getMin_vcore());// min_vcore
				psts.setString(10, queue.getMax_memory());// max_memory
				psts.setString(11, queue.getMax_vcore());// max_vcore
				psts.addBatch(); // 加入批量处理
				count++;
			}
		}
		psts.executeBatch(); // 执行批量处理
		conn.commit(); // 提交
		log.info("queue信息成功写入mysql");
		psts.close();
		log.info("本次导入的queue信息总条数: " + count);

	}

	private static void doJob(BufferedReader br, String insertSql, Connection conn) throws SQLException, IOException {
		conn.setAutoCommit(false); // 设置手动提交
		int count = 0;
		PreparedStatement psts = conn.prepareStatement(insertSql);
		log.info("准备执行的sql语句:" + insertSql);
		String line = null;
		while (null != (line = br.readLine())) {
			if (StringUtils.isBlank(line)) {
				log.warn("本条数据为空");
				continue;
			}
			JobAlarmModel job = new JobAlarmModel(line);
			if (job != null && StringUtils.isNotBlank(job.getJob_id())) {
				psts.setString(1, job.getLog_time());// log_time
				psts.setString(2, "1");// type
				psts.setString(3, job.getJob_id());// job_id
				psts.setString(4, job.getJob_state());// job_state
				psts.setString(5, job.getStart_time());// start_time
				psts.setString(6, job.getUser_name());// user_name
				psts.setString(7, job.getQueue());// queue
				psts.setString(8, job.getPriority());// priority
				psts.setString(9, job.getUsed_containers());// used_containers
				psts.setString(10, job.getRsvd_containers());// rsvd_containers
				psts.setString(11, job.getUser_name());// used_mem
				psts.setString(12, job.getRsvd_mem());// rsvd_mem
				psts.setString(13, job.getNeeded_mem());// needed_mem
				psts.setString(14, job.getAm_info());// am_info
				psts.setString(15, job.getApplication_id());// application_id
				psts.setString(16, job.getApplication_name());// application_name
				psts.setString(17, job.getApplication_type());// application_type
				psts.setString(18, job.getApplication_state());// application_state
				psts.setString(19, job.getApplication_final_state());// application_final_state
				psts.setString(20, job.getApplication_progress());// application_progress
				psts.setString(21, job.getApplication_tracking_url());// application_tracking_url
				psts.addBatch(); // 加入批量处理
				count++;
			}
		}
		psts.executeBatch(); // 执行批量处理
		conn.commit(); // 提交
		log.info("job信息成功写入mysql");
		psts.close();
		log.info("本次导入的job信息总条数: " + count);

	}
}
